﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace WF.Web.Controls
{
    public class CCHRDropDownList : DropDownList
    {
        public string ExceptionItemText
        {
            get
            {
                if (ViewState["ExceptionItemText"] == null)
                    return "---未知类型---";
                return ViewState["ExceptionItemText"].ToString();
            }
            set
            {
                ViewState["ExceptionItemText"] = value;
            }
        }

        public string ExceptionItemValue
        {
            get
            {
                if (ViewState["ExceptionItemValue"] == null)
                    return "0";
                return ViewState["ExceptionItemValue"].ToString();
            }
            set
            {
                ViewState["ExceptionItemValue"] = value;
            }
        }

        public override string SelectedValue
        {
            get
            {
                return base.SelectedValue;
            }
            set
            {
                try
                {
                    base.SelectedValue = value;
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    //ListItem itm = new ListItem(ExceptionItemText, ExceptionItemValue);
                    ListItem itm = new ListItem(ExceptionItemText, this.SelectedValue);
                    Items.Insert(0, itm);
                    SelectedIndex = 0;
                }
            }
        }

        protected override void OnDataBinding(EventArgs e)
        {
            try
            {
                base.OnDataBinding(e);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                //ListItem itm = new ListItem(ExceptionItemText, ExceptionItemValue);
                ListItem itm = new ListItem(ExceptionItemText, this.SelectedValue);
                Items.Insert(0, itm);
                SelectedIndex = 0;
            }
        }
    }
}